package com.ruoyi.park.utils;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class POJOConverter {
    /**
     * 对象List复制
     *
     * @param ss
     * @param cls
     * @param <S>
     * @param <T>
     * @return
     */
    public static <S, T> List<T> copyList(List<S> ss, Class<T> cls) {

        List<T> tRes = new ArrayList<>(ss.size());
        try {
            for (S s : ss) {
                T t = cls.newInstance();
                BeanUtil.copyProperties(s, t);
                tRes.add(t);
            }
        } catch (Exception e) {
            log.info("类型转换异常，异常信息: ", e);
        }

        return tRes;
    }

    public static <S,T> T copyObject(S ss,Class<T> cls){
        T t = null;
        try {
            t = cls.newInstance();
            BeanUtils.copyProperties(ss,t);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return t;
    }

//    public static <T, K, U> Map<K,U> list2Map(List<U> source , Function<? super T, ? extends K> keyMapper,Function<? super T, ? extends U> valueMapper){
//        return source.stream().collect(Collectors.toMap(keyMapper,valueMapper));
//    }

}
